package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.WorkRequestListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.WrokRequestList;
import com.isunland.managesystem.entity.WrokRequestList.WorkRequestDetail;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 办公申请列表
 *
 * @author wu
 */
public class WorkRequestListFragment extends BaseListFragment {

  private static final int REQUEST_EDIT = 0;
  private static final int REQUEST_ADD = 1;
  private static final int REQUEST_QUERY = 2;
  public static final String REQUEST_STARTTIME =
      "com.isunland.managesystem.ui.REQUEST_WORK_START_DATE";
  public static final String REQUEST_ENDTIME = "com.isunland.managesystem.ui.REQUEST_ENDTIME";
  public static final String REQUEST_DATASTATUS = "com.isunland.managesystem.ui.REQUEST_DATASTATUS";
  private WorkRequestListAdapter mAdapter;
  private String mStartTime;
  private String mEndTime;
  private String mDataStatus;

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标
    setBack(true);
    setTitleCustom(R.string.workReusest);
    mStartTime = MyDateUtil.format(MyDateUtil.getBeforeMonth(1), MyDateUtil.TYPE_DATE);
    mEndTime = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    mDataStatus = "";
  }

  @Override protected String setEmptyText() {
    return getResources().getString(R.string.notAnyCorporation);
  }

  @Override public String getUrl() {
    return ApiConst.URL_GET_WORKREQUEST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("orderSeq", "desc");
    params.put("orderField", "REG_DATE");
    params.put("regStaffId", mCurrentUser.getJobNumber());
    params.put("beginregDate", mStartTime);
    params.put("endregDate", MyStringUtil.connect(mEndTime, " 23:59:59"));
    params.put("dataStatus", mDataStatus);

    return params;
  }

  @Override public void resolveResponse(String json) {
    WrokRequestList requestList = new Gson().fromJson(json, WrokRequestList.class);
    ArrayList<WorkRequestDetail> detail = requestList.getRows();
    setAdapter(detail);
  }

  private void setAdapter(ArrayList<WorkRequestDetail> detail) {
    if (detail == null) {
      detail = new ArrayList<>();
    }
    mAdapter = new WorkRequestListAdapter(mActivity, detail);
    setListAdapter(mAdapter);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    WorkRequestDetail detail = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), WorkRequestDetailActivity.class);
    intent.putExtra(WorkRequestDetailFragment.EXTRA_VALUE, detail);
    startActivityForResult(intent, REQUEST_EDIT);
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        getActivity().finish();
        break;

      case R.id.menu_item_add:
        intent = new Intent(getActivity(), WorkRequestAddActivity.class);
        startActivityForResult(intent, REQUEST_ADD);
        break;

      case R.id.menu_item_querys:
        intent = new Intent(getActivity(), WorkRequestQueryActivity.class);
        intent.putExtra(WorkRequestQueryFragment.REQUEST_STRAT_DATE, mStartTime);
        intent.putExtra(WorkRequestQueryFragment.REQUEST_END_DATE, mEndTime);
        intent.putExtra(WorkRequestQueryFragment.REQUEST_DATA_STATUS, mDataStatus);
        startActivityForResult(intent, REQUEST_QUERY);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_search, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_ADD && data != null) {
      volleyPost();
    }
    if (requestCode == REQUEST_QUERY && data != null) {
      mStartTime = data.getStringExtra(REQUEST_STARTTIME);
      mEndTime = data.getStringExtra(REQUEST_ENDTIME);
      mDataStatus = data.getStringExtra(REQUEST_DATASTATUS);
    }
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
